import java.util.Random;
import java.util.concurrent.CountDownLatch;

/*
 * @author zzr
 * @date: 2025/05/11  20:38
 * @description:  CountDownLatch 使用示例~
 */
public class Demo19 {
    public static void main(String[] args) throws InterruptedException {
        CountDownLatch count = new CountDownLatch(10);
        for (int i = 0; i < 10; i++) {
            int id = i;
            Thread thread = new Thread(() -> {
                try {
                    Random random = new Random();
                    int time = random.nextInt((10) + 1) * 1000;
                    System.out.println("线程 " + id + "开始下载");
                    Thread.sleep(time);
                    System.out.println("线程 " + id + "结束下载");

                    // 告知 count 执行结束了
                    count.countDown();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            });

            thread.start();
        }
        // await 操作来等待所有任务结束
        count.await();
        System.out.println("所有任务完成了");
    }
}
